<%--
  Created by IntelliJ IDEA.
  User: 韩金龙
  Date: 2021/11/23
  Time: 9:07
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <script src="js/jquery-1.8.2.min.js"></script>
    <script src="js/fileupload.js"></script>
    <link rel="stylesheet" href="css/css.css">
    <link rel="stylesheet" href="css/img.css">
</head>
<body>
    添加
    <form>
        地区:<input type="text" name="title"><br>
        图片:<div class="photoUpLoad" >
        <input type="file"   class="photo" name="photo" accept="image/*"><span class="cross">+</span>
        <img src="" alt="" class="photoEcho">
        </div>
        <br>
        省:<select name="shengid"><option value="">请选择</option> </select><br>
        市:<select name="shiid"><option value="">请选择</option> </select><br>
        县:<select name="xianid"><option value="">请选择</option> </select><br>
    </form>
    <button onclick="add()">添加</button>
</body>
<script>
    $(function () {
        $.ajax({
            url:"cityList.do",
            type:"post",
            data:{"id":0},
            dataType:"json",
            success:function(obj) {
                console.log(obj)
                for (let i in obj) {
                    $("[name='shengid']").append('<option value='+obj[i].id+'>'+obj[i].name+'</option>')
                }
            }
        })
        $("[name='shengid']").change(function () {
            var id = $("[name='shengid']").val();
            $("[name='shiid']").empty()
            $("[name='shiid']").append("<option value=\"\">请选择</option>")
            $("[name='xianid']").empty()
            $("[name='xianid']").append("<option value=\"\">请选择</option>")
            $.ajax({
                url:"cityList.do",
                type:"post",
                data:{"id":id},
                dataType:"json",
                success:function(obj) {
                    console.log(obj)
                    for (let i in obj) {
                        $("[name='shiid']").append('<option value='+obj[i].id+'>'+obj[i].name+'</option>')
                    }
                }
            })
        })
        $("[name='shiid']").change(function () {
            var id = $("[name='shiid']").val();
            $("[name='xianid']").empty()
            $("[name='xianid']").append("<option value=\"\">请选择</option>")
            $.ajax({
                url:"cityList.do",
                type:"post",
                data:{"id":id},
                dataType:"json",
                success:function(obj) {
                    console.log(obj)
                    for (let i in obj) {
                        $("[name='xianid']").append('<option value='+obj[i].id+'>'+obj[i].name+'</option>')
                    }
                }
            })
        })
    })
    function add() {
        var formData = new FormData($("form")[0]);
        $.ajax({
            url:"add.do",
            type:"post",
            data:formData,
            dataType:"json",
            success:function(obj) {
                if (obj>0){
                    alert("添加成功!!")
                    location="list.do"
                }
            },
            processData:false,
            contentType:false,
        })
    }
</script>
</html>
